package cn.gson.springboot.model.service.afterservice;

import cn.gson.springboot.model.dao.afterdao.CheckDetailsDao;
import cn.gson.springboot.model.dao.afterdao.CucheckDao;
import cn.gson.springboot.model.dao.afterdao.EvectionDao;
import cn.gson.springboot.model.dao.afterdao.ExpenseDao;
import cn.gson.springboot.model.dao.presaledao.OfferDao;
import cn.gson.springboot.model.dao.saledao.ContractDao;
import cn.gson.springboot.model.mapper.aftermapper.CucheckMapper;
import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.vo.aftervo.CucheckVo;
import cn.gson.springboot.model.vo.aftervo.ExpenseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class CucheckService {

    @Autowired
    CucheckDao dao;
    @Autowired
    CucheckMapper mapper;
    @Autowired
    ExpenseDao ExpsDao;
    @Autowired
    EvectionDao EvetDao;
    @Autowired
    ContractDao contractDao;
    @Autowired
    OfferDao offerDao;
    @Autowired
    CheckDetailsDao checkDetailsDao;

    //审批表分页
    public List<CucheckVo> selectCucheckPage(Integer cutId, String apprTitle, Integer empId){
        return mapper.checkPage(cutId, apprTitle, empId);
    }

    //审批类型查询
    public List<CucheckType> selectCheckTypeAll(){
        return mapper.checkTypeAll();
    }

    //同意，并结束审批
    public void addCheck(Integer empId, Integer apprId, String apprView, Integer trialId, String title){
        Timestamp time = new Timestamp(new Date().getTime());
        CucheckType type = new CucheckType();
        type.setCutId(4);
        CheckDetails details = new CheckDetails();
        Emp empOne = new Emp();
        empOne.setEmployeesId(empId);
        details.setApprId(apprId);
        details.setApprView(apprView);
        details.setApprsDate(time);
        details.setEmpByEmployeesId(empOne);
        details.setCucheckTypeByCutId(type);
        checkDetailsDao.save(details);
        Cucheck check = dao.findById(apprId).get();
        check.setApprView(apprView);
        Emp emp = new Emp();
        emp.setEmployeesId(empId);
        check.setEmpByEmployeesId(emp);
        check.setCucheckTypeByCutId(type);
        check.setApprComplete(1);
        dao.save(check);
        if(title.equals("报价审批")){
            Offer offer = offerDao.findById(trialId).get();
            offer.setApprovaler(empId);
            offer.setOfferStatus(4);
            offerDao.save(offer);
        }
        if(title.equals("合同审批")){
            Contract cont = contractDao.findById(trialId).get();
            cont.setContractApproverid(empId);
            cont.setContractAppstate(4);
            contractDao.save(cont);
        }
        if(title.equals("费用报销")){
            Expense expense = ExpsDao.findById(trialId).get();
            expense.setEmpId(empId);
            expense.setCucheckTypeByCutId(type);
            ExpsDao.save(expense);
        }
        if(title.equals("出差申请")){
            Evection evection = EvetDao.findById(trialId).get();
            evection.setEvetEmpid(empId);
            evection.setEvetStatus("同意");
            evection.setEvetState("出差中");
            EvetDao.save(evection);
        }
    }

    //同意，并继续下一个审批人
    public void deliver(Integer empId, Integer apprId, Integer employeesId, String apprView, Integer trialId, String title){
        Timestamp time = new Timestamp(new Date().getTime());
        CucheckType type = new CucheckType();
        type.setCutId(4);
        CheckDetails details = new CheckDetails();
        Emp empOne = new Emp();
        empOne.setEmployeesId(empId);
        details.setApprId(apprId);
        details.setApprView(apprView);
        details.setApprsDate(time);
        details.setEmpByEmployeesId(empOne);
        details.setCucheckTypeByCutId(type);
        checkDetailsDao.save(details);
        Cucheck check = dao.findById(apprId).get();
        check.setApprView(apprView);
        Emp emp = new Emp();
        check.setCucheckTypeByCutId(type);
        emp.setEmployeesId(employeesId);
        check.setEmpByEmployeesId(emp);
        dao.save(check);
        if(title.equals("报价审批")){
            Offer offer = offerDao.findById(trialId).get();
            offer.setApprovaler(employeesId);
            offer.setOfferStatus(3);
            offerDao.save(offer);
        }
        if(title.equals("合同审批")){
            Contract cont = contractDao.findById(trialId).get();
            cont.setContractApproverid(employeesId);
            cont.setContractAppstate(3);
            contractDao.save(cont);
        }
        if(title.equals("费用报销")){
            Expense expense = ExpsDao.findById(trialId).get();
            CucheckType types = new CucheckType();
            types.setCutId(3);
            expense.setEmpId(employeesId);
            expense.setCucheckTypeByCutId(types);
            ExpsDao.save(expense);
        }
        if(title.equals("出差申请")){
            Evection evection = EvetDao.findById(trialId).get();
            evection.setEvetEmpid(employeesId);
            evection.setEvetStatus("审批中");
            EvetDao.save(evection);
        }
    }

    //否决
    public void delete(Integer empId, Integer apprId, String apprView, Integer trialId, String title){
        Timestamp time = new Timestamp(new Date().getTime());
        CucheckType type = new CucheckType();
        type.setCutId(5);
        CheckDetails details = new CheckDetails();
        Emp empOne = new Emp();
        empOne.setEmployeesId(empId);
        details.setApprId(apprId);
        details.setApprsDate(time);
        details.setApprView(apprView);
        details.setEmpByEmployeesId(empOne);
        details.setCucheckTypeByCutId(type);
        checkDetailsDao.save(details);
        Cucheck check = dao.findById(apprId).get();
        Emp emp = new Emp();
        emp.setEmployeesId(empId);
        check.setCucheckTypeByCutId(type);
        check.setApprView(apprView);
        check.setEmpByEmployeesId(emp);
        check.setApprComplete(1);
        dao.save(check);
        if(title.equals("报价审批")){
            Offer offer = offerDao.findById(trialId).get();
            offer.setApprovaler(empId);
            offer.setOfferStatus(5);
            offerDao.save(offer);
        }
        if(title.equals("合同审批")){
            Contract cont = contractDao.findById(trialId).get();
            cont.setContractApproverid(empId);
            cont.setContractAppstate(5);
            contractDao.save(cont);
        }
        if(title.equals("费用报销")){
            Expense expense = ExpsDao.findById(trialId).get();
            expense.setEmpId(empId);
            expense.setCucheckTypeByCutId(type);
            ExpsDao.save(expense);
        }
        if(title.equals("出差申请")){
            Evection evection = EvetDao.findById(trialId).get();
            evection.setEvetEmpid(empId);
            evection.setEvetStatus("否决");
            EvetDao.save(evection);
        }
    }

    //查询
    public CucheckVo check(Integer apprId){
        return mapper.checkByApprId(apprId);
    }

    //回显需审批的销售费用表
    public ExpenseVo checkExpsByApprId(Integer apprId){
        return mapper.expsByApprId(apprId);
    }

    //回显需审批的出差表
    public Evection checkEvetByApprId(Integer apprId){
        return mapper.evetByApprId(apprId);
    }

    //通过审批id找出审批类型
    public String titleApprId(Integer apprId){
        return mapper.titleByApprId(apprId);
    }

    //查询审批记录表
    public List<CheckDetails> selectCheckDetails(Integer apprId){
        return mapper.checkDetailsAll(apprId);
    }

    //通过审批id查询查询出需要审批的表id
    public Integer selectApprIdsById(Integer apprId){
        return mapper.apprIdsById(apprId);
    }

    //被审批表查询审批记录
    public List<CheckDetails> selectDeliverId(String apprTitle, Integer apprIds){
        return mapper.deliverById(apprTitle, apprIds);
    }

    //通过审批类型和被审批id
    public Integer selectCheckById(String apprTitle, Integer apprIds){
        return mapper.checkById(apprTitle, apprIds);
    }

    /*//销售费用新增审批
    public void addsExps(Integer expsId){
        Expense exps = ExpsDao.findById(expsId).get();
        String data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(exps.getExpsDate());
        String date = data.substring(0,10);
        Cucheck check = new Cucheck();
        check.setApprIds(expsId);//需要审批的id
        check.setApprTitle("费用报销");//审批类型
        check.setApprReason("费用明细:"+"\t"+"￥"+exps.getExpsMoney()+"\t"+date+"\t"+exps.getExpsSort());
        check.setApprDate(exps.getExpsDate());//时间
        check.setApprPeople(exps.getEmpByEmployeesId().getEmployeesId());//申请人
        Emp emp = new Emp();
        emp.setEmployeesId(exps.getEmpId());
        check.setEmpByEmployeesId(emp);//审批人
        CucheckType type = new CucheckType();
        type.setCutId(2);
        check.setCucheckTypeByCutId(type);
        dao.save(check);
    }

    //出差新增审批
    public void addsEvet(Integer evetId){
        Evection evet = EvetDao.findById(evetId).get();
        Cucheck check = new Cucheck();
        check.setApprIds(evetId);//需要审批的id
        check.setApprTitle("出差申请");
        check.setApprReason(evet.getEvetSubject());
        Timestamp time = new Timestamp(new Date().getTime());
        check.setApprDate(time);//时间
        check.setApprPeople(evet.getEmpByEmployeesId().getEmployeesId());//申请人
        Emp emp = new Emp();
        emp.setEmployeesId(evet.getEvetEmpid());
        check.setEmpByEmployeesId(emp);//审批人
        CucheckType type = new CucheckType();
        type.setCutId(2);
        check.setCucheckTypeByCutId(type);
        dao.save(check);
    }*/
}
